package com.meiqiongproperty.yiguanjia.fragment.user;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.RegisterLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendMsg;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendRegister;
import com.meiqiongproperty.yiguanjia.httpbean.user.RegiserBean;
import com.meiqiongproperty.yiguanjia.httpbean.user.SmSCode;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.utils.CountDownTimerUtils;
import com.meiqiongproperty.yiguanjia.utils.DeviceUtil;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.user
 * @User ChenJing
 * @Date 2017/4/25
 */

public class RegisterFragment extends BaseFragment {
    RegisterLayoutBinding binding;
    private String phone, real_name, verify_code, password, house,id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.register_layout, container, false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
//        binding.navigation.setTitle("注册");
//        binding.navigation.setLeftClick(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                sendKeyBackEvent();
//            }
//        });
        binding.imgTopBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
    }

    public void getCode_Click(View view) {
        if (isClikVualuePhone()) {
            getCode();
        }
    }

    public void commitClick(View view) {
        if (isClikVualue()) {
            doRegister();
        }
    }

    public void wyxqClick(View view) {
        TenementAddress address=new TenementAddress();
        address.setData(new TenementAddress.IOnItemListener() {
            @Override
            public void setListData(String address,String houseId) {
                binding.house.setText(address);
                id=houseId;
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), address, address.getTagName());
    }

    public boolean isClikVualuePhone() {
        phone = binding.inputPhone.getEditableText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    /**
     * 获取验证码
     */
    public void getCode() {
        //type 登录：LOGIN 注册：REGISTER 找回密码：RESETPASSWORD
        SendMsg sendmsg = new SendMsg();
        sendmsg.setMobile(phone);
        sendmsg.setType("REGISTER");
        ApiService.sendMsg(getActivity(), new SimpleHttpCallback<SmSCode>() {
            @Override
            public void onNext(SmSCode smsCode) {
                LogUtil.d("smsCode=" + smsCode.getCode());
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(binding.getVerifyCodeBtn, 60000, 1000);
                mCountDownTimerUtils.start();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, Utils.getRequestBody(sendmsg));
    }


    //发送注册请求
    private void doRegister() {
        SendRegister sendRegister = new SendRegister();
        sendRegister.setMobile(phone);
        sendRegister.setDevice(DeviceUtil.getIMEI(getActivity()));
        sendRegister.setPassword(password);
        sendRegister.setValidCode(verify_code);
        sendRegister.setRealName(real_name);
        sendRegister.setHousesId(id);
        ApiService.register(getActivity(), new SimpleHttpCallback<RegiserBean>() {
            @Override
            public void onNext(RegiserBean regiserBean) {
                LogUtil.d("smsCode=" + regiserBean.getUserinfo().toString());
                getActivity().finish();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, Utils.getRequestBody(sendRegister));
    }


    public boolean isClikVualue() {
        phone = binding.inputPhone.getEditableText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(getActivity(), "请输入手机号", Toast.LENGTH_LONG).show();
            return false;
        }
        real_name = binding.realName.getEditableText().toString();
        if (TextUtils.isEmpty(real_name)) {
            Toast.makeText(getActivity(), "请输入您的真实姓名", Toast.LENGTH_LONG).show();
            return false;
        }
        verify_code = binding.verifyCode.getEditableText().toString();
        if (TextUtils.isEmpty(verify_code)) {
            Toast.makeText(getActivity(), "请输入验证码", Toast.LENGTH_LONG).show();
            return false;
        }
        password = binding.password.getEditableText().toString();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(getActivity(), "请输入密碼", Toast.LENGTH_LONG).show();
            return false;
        }
        house = binding.house.getText().toString();
        if (TextUtils.isEmpty(house)) {
            Toast.makeText(getActivity(), "请选择小区", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
}
